﻿<UserControl x:Class="InteractivityQuickstart.Views.InvokeCommandActionView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:prism="http://www.codeplex.com/prism"
             xmlns:viewModels="clr-namespace:InteractivityQuickstart.ViewModels"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.DataContext>
        <viewModels:InvokeCommandActionViewModel />
    </UserControl.DataContext>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        
        <StackPanel Grid.Row="0">
            <TextBlock Margin="5" FontSize="24" Foreground="DarkRed" TextWrapping="Wrap">InvokeCommandAction</TextBlock>
            <TextBlock Margin="5" TextWrapping="Wrap">The <Bold>InvokeCommandAction</Bold> is useful when you need to invoke a command in response to an event raised by a control in the view.</TextBlock>
            <TextBlock Margin="5" TextWrapping="Wrap">
                In the following example there is a list of items and we want to execute a command in the view model when an item is selected. 
                The view model will then change the "Selected Item" shown below.
            </TextBlock>
        </StackPanel>

        <ListBox Grid.Row="1" Margin="5" ItemsSource="{Binding Items}" SelectionMode="Single">
            <i:Interaction.Triggers>
                <!-- This event trigger will execute the action when the corresponding event is raised by the ListBox. -->
                <i:EventTrigger EventName="SelectionChanged">
                    <!-- This action will invoke the selected command in the view model and pass the parameters of the event to it. -->
                    <prism:InvokeCommandAction Command="{Binding SelectedCommand}" TriggerParameterPath="AddedItems" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ListBox>

        <StackPanel Grid.Row="2" Margin="5" Orientation="Horizontal">
            <TextBlock Foreground="DarkRed" FontWeight="Bold">Selected Item: </TextBlock>
            <TextBlock AutomationProperties.AutomationId="SelectedItemTextBlock" Foreground="DarkRed" FontWeight="Bold" Margin="5,0" Text="{Binding SelectedItemText}" />
        </StackPanel>
        
    </Grid>
</UserControl>
